<?php 
require_once 'PHPExcel.php';
$table=$_REQUEST[table];
$rows=$_REQUEST[rows];
$man=$_REQUEST[man];
output($table,$rows,$man);
// 首先创建一个新的对象  PHPExcel object
function output($table,$rows,$man){
$objPHPExcel = new PHPExcel();
$last_rows=$rows;
$next_last_rows=$last_rows+1;
$total_rows=$last_rows+6;
$table=explode("---", $table);
$col=7;
// 设置文件的一些属性，在xls文件——>属性——>详细信息里可以看到这些值，xml表格里是没有这些值的
// 位置aaa  *为下文代码位置提供锚
// 给表格添加数据
$objPHPExcel->setActiveSheetIndex(0);  //设置第一个内置表（一个xls文件里可以有多个表）为活动的
//             ->setCellValue( 'A1', 'Hello' )         //给表的单元格设置数据
//             ->setCellValue( 'B2', 'world!' )      //数据格式可以为字符串
//             ->setCellValue( 'C1', 12)            //数字型
//             ->setCellValue( 'D2', 12)            //
//             ->setCellValue( 'D3', true )           //布尔型
//             ->setCellValue( 'D4', '=SUM(C1:D2)' )//公式
// 			->mergeCells('B3:C22')
// 			->setCellValue('B21','longer');

//得到当前活动的表,注意下文教程中会经常用到$objActSheet
$objActSheet = $objPHPExcel->getActiveSheet();
// 位置bbb  *为下文代码位置提供锚
// 给当前活动的表设置名称



$objActSheet->setTitle('longer');
$objStyleA5 = $objActSheet->getStyle('A5');
$objBorderA5 = $objStyleA5->getBorders();
$objBorderA5->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
$objBorderA5->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
$objBorderA5->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
$objBorderA5->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
$objActSheet->duplicateStyle($objStyleA5, 'A1:G'.$total_rows);

//设置字体
$objStyleA1 = $objActSheet->getStyle('A1');
$objFontA1 = $objStyleA1->getFont();
$objFontA1->setName('黑体');
$objFontA1->setSize(16);
$objFontA1->setBold(true);
$objAlignA1=$objStyleA1->getAlignment();
$objAlignA1->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objAlignA1->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$objStyleA2 = $objActSheet->getStyle('A2');
$objFontA2 = $objStyleA2->getFont();
$objFontA2->setName('宋体');
$objFontA2->setSize(12);
$objActSheet->duplicateStyle($objStyleA2, 'A2:G'.$total_rows);

//设置对齐方式
$objAlignA5 = $objStyleA5->getAlignment();
$objAlignA5->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objAlignA5->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

$objStyleA19 = $objActSheet->getStyle('A'.$last_rows);
$objAlignA19 = $objStyleA5->getAlignment();
$objAlignA19->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objAlignA19->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->setCellValue('A'.$last_rows,'本页合计：');

$objStyleA6 = $objActSheet->getStyle('A6');
$objAlignA6 = $objStyleA5->getAlignment();
$objAlignA6->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objAlignA6->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->duplicateStyle($objStyleA6, 'A3:G4');

$objStyleB20 = $objActSheet->getStyle('B20');
$objAlignB20 = $objStyleB20->getAlignment();
$objAlignB20->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objAlignB20->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);


$objActSheet->mergeCells('A1:G1');
$objActSheet->setCellValue('A1','云南大学图书馆勤工助学考勤表');

$objActSheet->setCellValue('A2','用工部门：图书馆');
$objActSheet->setCellValue('G2',$man);
$objActSheet->setCellValue('G3','金额（元）');
$objActSheet->setCellValue('A3','编号');
$objActSheet->setCellValue('B3','学院');
$objActSheet->setCellValue('C3','学号');
$objActSheet->setCellValue('D3','姓名');
$objActSheet->setCellValue('E3','勤工助学用工');
$objActSheet->setCellValue('E4','时间段');
$objActSheet->getStyle('F4')->getAlignment()->setWrapText(true);
$objActSheet->setCellValue('F4','总时间（天）');

$objStyleA20 = $objActSheet->getStyle('A'.$next_last_rows);
$objAlignA20 = $objStyleB20->getAlignment();
$objAlignA20->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objAlignA20->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('A'.$next_last_rows)->getAlignment()->setWrapText(true);

// $objStyleB20 = $objActSheet->getStyle('A20');
// $objBorderB20 = $objStyleB20->getBorders();
// $objBorderB20->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
// $objBorderB20->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
// $objBorderB20->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
// $objBorderB20->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
// $objActSheet->duplicateStyle($objStyleB20, 'B20:G25');


$objActSheet->setCellValue('A'.$next_last_rows,'用工部门意见：');
$objActSheet->getStyle('B'.$next_last_rows)->getAlignment()->setWrapText(true);
$objActSheet->setCellValue('B'.$next_last_rows,"签字：\t\t\t\t\t\t 盖章                                                                                                                                                                                                                                           
                                             年          月        日");

$objActSheet->mergeCells('A2:F2');
$objActSheet->mergeCells('A3:A4');
$objActSheet->mergeCells('B3:B4');
$objActSheet->mergeCells('C3:C4');
$objActSheet->mergeCells('D3:D4');
$objActSheet->mergeCells('E3:F3');
$objActSheet->mergeCells('G3:G4');
$objActSheet->mergeCells('A'.$last_rows.':E'.$last_rows);
$objActSheet->mergeCells('A'.$next_last_rows.':A'.$total_rows);
$objActSheet->mergeCells('B'.$next_last_rows.':G'.$total_rows);

$objActSheet->getRowDimension(1)->setRowHeight(39);
$objActSheet->getRowDimension(2)->setRowHeight(30);
$objActSheet->getRowDimension(3)->setRowHeight(30);
$objActSheet->getRowDimension(4)->setRowHeight(45);
$objActSheet->getColumnDimension('A')->setWidth(6);
$objActSheet->getColumnDimension('B')->setWidth(25);
$objActSheet->getColumnDimension('C')->setWidth(15);
$objActSheet->getColumnDimension('D')->setWidth(12);
$objActSheet->getColumnDimension('E')->setWidth(12);
$objActSheet->getColumnDimension('F')->setWidth(12);
$objActSheet->getColumnDimension('G')->setWidth(20);

for($i=5;$i<$rows;$i++){
	for($j=0;$j<$col;$j++){
		switch($j){
			case 0:
				$objActSheet->setCellValue('A'.$i,$table[($i-4)*$col+$j]);
				break;
			case 1:
				$objActSheet->setCellValue('B'.$i,$table[($i-4)*$col+$j]);
				break;
			case 2:
				$objActSheet->setCellValue('C'.$i,$table[($i-4)*$col+$j]);
				break;
			case 3:
				$objActSheet->setCellValue('D'.$i,$table[($i-4)*$col+$j]);
				break;
			case 4:
				$objActSheet->setCellValue('E'.$i,$table[($i-4)*$col+$j]);
				break;
			case 5:
				$objActSheet->setCellValue('F'.$i,$table[($i-4)*$col+$j]);
				break;
			case 6:
				$objActSheet->setCellValue('G'.$i,$table[($i-4)*$col+$j]);
				break;
		}			
	}
}



header('Content-type:application/vnd.ms-excel;charset=utf-8;');
header('Content-Disposition: attachment;filename="longer.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
print_r($objPHPExcel);die("aaa");
}
exit;
?>